/**
 *
 */
package com.bobo.web.provider.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author yangbo
 * @date 2018-10-22
 * @filename RedisUtils.java
 */
@Component
public class VoteRedisUtils {
    private Logger LOGGER = LoggerFactory.getLogger(VoteRedisUtils.class.getSimpleName());
    @Resource
    private RedisUtils redisUtils;

    /**
     * 防刷、限流 test
     *
     * @param path
     * @param num
     * @param expireTime 秒
     * @return
     */
    public Integer getLimitNum(String path, int num, int expireTime) {
        try {
            if (redisUtils.increment(path, num) == 1) {
                redisUtils.expire(path, expireTime, TimeUnit.SECONDS);
            }
            return (Integer) redisUtils.getValueBykeys(path);
        } catch (Exception e) {
            LOGGER.error(String.format("getLimitNum path redis error path:{}", path), e);
            return 1;
        }

    }

}
